<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="成果名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入成果名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="应用领域" name="applicationField">
            <t-select style="width: 240px" clearable v-model="queryParams.applicationField"
              :options="dict.type.application_field">
            </t-select>
          </t-form-item>
          <t-form-item label="创新类别" name="innovationCategory">
            <t-select style="width: 240px" clearable v-model="queryParams.innovationCategory"
              :options="dict.type.innovation_category">
            </t-select>
          </t-form-item>
          <t-form-item label="成果状态" name="innovationStatus">
            <t-select style="width: 240px" clearable v-model="queryParams.innovationStatus"
              :options="dict.type.innovation_status">
            </t-select>
          </t-form-item>
          <t-form-item label="主要完成人员" name="leaderName">
            <t-input v-model="queryParams.leaderName" clearable placeholder="请输入全名" style="width: 240px" />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="apply()" variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        申报
      </t-button>
    </div>
    <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination" :loading="dataLoading">
      <template #name="{ row }">
        <t-link @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_innovation_detail = true" hover="color"
          theme="primary">{{ row.name }}</t-link>
      </template>
      <template #applicationField="{ row }">
        {{ row.applicationField | filterByDict(dict.type.application_field) }}
      </template>
      <template #innovationCategory="{ row }">
        {{ row.innovationCategory | filterByDict(dict.type.innovation_category) }}
      </template>
      <template #innovationStatus="{ row }">
        <t-tag @click="dialog.header = '审批记录'; dialog.objId = row.id; dialog.open_approve_list = true" class="cursor-pointer"
          :theme="handleTheme(row.innovationStatus, dict.type.innovation_status)" variant="light">
          {{ row.innovationStatus | filterByDict(dict.type.innovation_status) }}
        </t-tag>
      </template>
      <template #op="{ row }">
        <t-button v-if="['1', '4', '6', null].includes(row.innovationStatus)" @click="apply(row.id)" size="small"
          variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          {{ ['4', '9'].includes(row.innovationStatus) ? '修改' : '继续申报' }}
        </t-button>
        <t-button @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_innovation_detail = true" size="small"
          variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <t-button @click="createPDF(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon" />
          导出PDF
        </t-button>
        <t-button v-if="['1', '4', '6'].includes(row.innovationStatus)" theme="danger" @click="deleteProject(row)"
          size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>

      </template>
    </t-table>
    <innovation-detail-dialog :header="dialog.header" :objId="dialog.objId"
      :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>
  </div>
</template>

<script>
import InnovationDetailDialog from "../innovation/components/innovation-detail-dialog.vue";
import ApproveList from "../approve/components/approve-list.vue";

export default {
  name: "innovation-my",
  dicts: ["application_field", "innovation_category", "approve_status", "innovation_status"],
  components: {
    InnovationDetailDialog,
    ApproveList
  },
  data () {
    return {
      dialog: {
        header: "",
        objId: 0,
        open_innovation_detail: false,
        open_approve_list: false,
      },
      data: [],
      columns: [
        {
          colKey: "name",
          title: "成果名称",
          align: "center",
          width: 220
        },
        {
          colKey: "applicationProject",
          title: "应用项目",
          align: "center",
        },
        {
          colKey: "applicationField",
          title: "应用领域",
          align: "center",
        },
        {
          colKey: "innovationCategory",
          title: "创新类别",
          align: "center",
        },
        {
          colKey: "leaderName",
          title: "负责人",
          align: "center",
        },
        {
          colKey: "deptName",
          title: "工作单位",
          align: "center",
        },
        {
          colKey: "innovationStatus",
          title: "成果状态",
          align: "center",
          width: 120
        },
        {
          colKey: "createTime",
          title: "申报时间",
          align: "center",
          width: 200
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationField: '',
        innovationCategory: '',
        name: '',
        leaderName: '',
        innovationStatus: '',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created () {
    this.getList();
  },
  methods: {
    createPDF (row) {
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.wcx.apply.innovationPdf(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    getList () {
      this.dataLoading = true;
      this.$api.wcx.apply.innovationMy(this.queryParams).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
          this.dataLoading = false;
          this.$message.error(e.toString());
        });
    },
    apply (id) {
      if (id) { // 编辑
        sessionStorage.setItem(`${id}-wcxInnovationId`, id);
        this.$router.push(`/wcx/other/apply/innovation-apply/${id}`);
      } else {
        this.$router.push(`/wcx/other/apply/innovation-apply/${Date.parse(new Date())}`);
      }
    },
    deleteProject (row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除成果名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.wcx.apply.innovationDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    },
  }
}
</script>
